/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.krossk.servlets;

import br.com.krossk.dao.ArquivoDAO;
import br.com.krossk.jpa.JPAFactory;
import br.com.krossk.model.Area;
import br.com.krossk.model.Arquivo;
import br.com.krossk.util.DateManager;
import java.io.IOException;
import java.io.PrintWriter;
import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Henrique
 */
public class DetalharArquivo extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=Windows-1252");
        PrintWriter out = response.getWriter();
        int idArquivo = Integer.parseInt(request.getParameter("idArquivo"));
        EntityManager em = JPAFactory.getEntityManager();
        Arquivo a =  em.find(Arquivo.class, idArquivo);

        out.println("<html>");
        out.println("<head><title>Detalhes do Arquivo</title></head>");
        out.println("<body bgcolor='#ccccff'>");
        out.println("<div style='background-color:#eee; color:#009; font-size: 16px; font-family: Arial; font-weight: 700; text-align: center'>Detalhes do Arquivo</div><br />");
        out.println("<font face='Arial' style='font-size:14px; color:#009'>");
        out.println("Título: <b>"+a.getTitulo()+"</b><br />");
        out.println("Nome do arquivo: <b>"+a.getNomeArquivo()+"</b><br />");
        out.println("Tamanho: <b>"+a.getTamanhoFormatado()+"</b><br />");
        out.println("Data de inclusão: <b>"+DateManager.dataNormal(a.getDataInclusao())+"</b><br />");
        out.println("Incluído por: <b>"+a.getUsuario().getNomeUsuario()+" (<i>"+a.getUsuario().getNickname()+"</i>)</b><br />");
        out.println("Quantidade de downloads: <b>"+a.getUsuarioCollection().size()+"</b><br />");
        out.println("Segmento acadêmico: <b>"+a.getSegmento().getNomeSegmento()+"</b><br />");
        out.println("Áreas realacionadas: - ");
        for(Area area: a.getAreaCollection()){
            out.print("<b>"+area.getNomeArea().toLowerCase()+"</b> - ");
        }
        out.println("</font>");
        out.println("</body>");
        out.println("</html>");
        out.close();
em.close();
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
